package com.example.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.home.activity.R;

import java.util.List;

public class GoodsListAdapter extends BaseAdapter {
    private Context ctx;
    private List<com.example.home.viewmodel.GoodsList.List> lists;



    public GoodsListAdapter(Context ctx, List<com.example.home.viewmodel.GoodsList.List> lists) {
        this.ctx = ctx;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        return this.lists.size();
    }

    @Override
    public Object getItem(int position) {
        return this.lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return this.lists.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.goodslist_list_content,null);
            viewHolder.goodslist_content_img = convertView.findViewById(R.id.goodslist_content_img);
            viewHolder.goodslist_content_name = convertView.findViewById(R.id.goodslist_content_name);
            viewHolder.goodslist_content_brief = convertView.findViewById(R.id.goodslist_content_brief);
            viewHolder.goodslist_content_price_count = convertView.findViewById(R.id.goodslist_content_price_count);
            viewHolder.goodslist_content_price_retail = convertView.findViewById(R.id.goodslist_content_price_retail);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.goodslist_content_name.setText(this.lists.get(position).getName());
        viewHolder.goodslist_content_brief.setText(this.lists.get(position).getBrief());
        viewHolder.goodslist_content_price_count.setText(this.lists.get(position).getCounterPrice().toString());
        viewHolder.goodslist_content_price_retail.setText(this.lists.get(position).getRetailPrice().toString());
        Glide.with(convertView.getContext())
                .load(this.lists.get(position).getPicUrl())
                .into(viewHolder.goodslist_content_img);

        return convertView;
    }

    static class ViewHolder {
        ImageView goodslist_content_img;
        TextView goodslist_content_name;
        TextView goodslist_content_brief;
        TextView goodslist_content_price_count;
        TextView goodslist_content_price_retail;
    }
}
